package com.jy.appmarket.cards;

import com.google.gson.JsonObject;
import com.jy.app.market.apk.type.AdsType;
import com.jy.app.market.apk.type.SecurityStatus;
import com.jy.app.market.idata.data.Apk;
import com.jy.appmarket.R;
import com.jy.appmarket.utils.IdataUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import it.gmariotti.cardslib.library.internal.Card;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2015年8月21日
 * @version 1.0
 */

public class AppDetailInfoUICard extends Card {
	private Apk mApk;
	private Drawable mGreen;
	// private Drawable mYellow;
	private Drawable mRed;

	public AppDetailInfoUICard(Context context, Apk apk) {
		super(context, R.layout.appdetail_info_card_layout);
		mApk = apk;
		mGreen = context.getResources().getDrawable(R.drawable.app_status_green);
		// mYellow =
		// context.getResources().getDrawable(R.drawable.app_status_yellow);
		mRed = context.getResources().getDrawable(R.drawable.app_status_red);
	}

	@Override
	public void setupInnerViewElements(ViewGroup parent, View view) {
		if (mApk == null) {
			return;
		}
		ImageView icon = parent.findViewById(R.id.appdetail_card_icon);
		TextView name = parent.findViewById(R.id.appdetail_card_name);
		TextView info = parent.findViewById(R.id.appdetail_card_info);
		JsonObject icons = mApk.getIcons();
		ImageLoader.getInstance().displayImage(IdataUtils.getIconUrl(icons), icon);
		name.setText(mApk.getTitle());
		String infoText = mApk.getInstalledCountStr() + "人安装  "
				+ Formatter.formatFileSize(getContext(), DownloadableUICard.safeInt(mApk.getBytes()));
		info.setText(infoText);

		TextView ad = parent.findViewById(R.id.app_status_ad);
		TextView safe = parent.findViewById(R.id.app_status_virus);
		// TextView paid = (TextView) parent.findViewById(R.id.app_status_pay);
		TextView official = parent.findViewById(R.id.app_status_official);
		if (mApk.getAdsType() == AdsType.NONE) {
			ad.setVisibility(View.VISIBLE);
			ad.setText(R.string.no_advertisement);
			ad.setCompoundDrawablesWithIntrinsicBounds(mGreen, null, null, null);
		} else {
			// ad.setText(R.string.has_advertisement);
			// ad.setCompoundDrawablesWithIntrinsicBounds(mRed, null, null,
			// null);

			ad.setVisibility(View.GONE);
		}

		if (mApk.getSecurityStatus() == SecurityStatus.DANGER) {
			safe.setVisibility(View.VISIBLE);
			safe.setText(R.string.danger);
			safe.setCompoundDrawablesWithIntrinsicBounds(mRed, null, null, null);
		} else if (mApk.getSecurityStatus() == SecurityStatus.SAFE) {
			safe.setVisibility(View.VISIBLE);
			safe.setText(R.string.safe);
			safe.setCompoundDrawablesWithIntrinsicBounds(mGreen, null, null, null);
		} else {
			// safe.setText(R.string.unknown_safe_state);
			// safe.setCompoundDrawablesWithIntrinsicBounds(mYellow, null, null,
			// null);
			safe.setVisibility(View.GONE);
		}

		// if (mApk.getPaidType() == PaidType.NONE) {
		// paid.setText(R.string.no_paid);
		// paid.setCompoundDrawablesWithIntrinsicBounds(mGreen, null, null,
		// null);
		// } else {
		// paid.setText(R.string.has_paid);
		// paid.setCompoundDrawablesWithIntrinsicBounds(mRed, null, null, null);
		// }

		if (mApk.isOfficial()) {
			official.setText(R.string.official_product);
			official.setCompoundDrawablesWithIntrinsicBounds(mGreen, null, null, null);
		} else {
			official.setVisibility(View.GONE);
		}

	}

	@Override
	public int getType() {
		return 6;
	}

	@Override
	public int getMarginTop() {
		return 0; // 返回0，则在顶部没有空隙
	}
}